package com.ctshk.sapp.system.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SupplierStatus;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.dto.QueryBigClientAndAgentDTO;
import com.ctshk.rpc.system.dto.SysBigClientDTO;
import com.ctshk.rpc.system.dto.SysBigClientQueryListDTO;
import com.ctshk.rpc.system.req.SysBigClientAddReq;
import com.ctshk.rpc.system.req.SysBigClientApprovalReq;
import com.ctshk.rpc.system.req.SysBigClientQueryListReq;
import com.ctshk.rpc.system.req.SysBigClientUpdateStatusReq;
import com.ctshk.rpc.system.service.ISysBigClientService;
import com.ctshk.rpc.system.service.ISysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 大客户 前端控制器
 * </p>
 *
 * @author 张志朋
 * @since 2021-02-03
 */
@Api(tags = "大客户")
@RestController
@RequestMapping("/sys/big/client")
public class SysBigClientController extends BaseController {

    @DubboReference
    private ISysBigClientService bigClientService;

    @DubboReference
    private ISysPermissionService sysPermissionService;

    @ApiOperation(value = "新增大客户(仅保存)", notes = "新增大客户(仅保存)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增大客户(仅保存)", operDesc = "新增大客户(仅保存)")
    @PostMapping("/addForSave")
    public Result addForSave(@RequestBody @Valid SysBigClientAddReq sysBigClientReq) {
        int addType = 1;
        return bigClientService.add(sysBigClientReq, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "新增大客户(走审批流程)", notes = "新增大客户(走审批流程)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增大客户(走审批流程)", operDesc = "新增大客户(走审批流程)")
    @PostMapping("/addForApproval")
    public Result addForApproval(@RequestBody @Valid SysBigClientAddReq sysBigClientReq) {
        int addType = 2;
        return bigClientService.add(sysBigClientReq, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "根据id查询大客户详情", notes = "根据id查询大客户详情")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "大客户详情", operDesc = "大客户详情")
    @GetMapping("/info/{id}")
    public Result<SysBigClientDTO> query(@PathVariable Long id) {
        return bigClientService.query(id);
    }

    @ApiOperation(value = "编辑大客户(仅保存)", notes = "编辑大客户(仅保存)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "编辑大客户(仅保存)", operDesc = "编辑大客户(仅保存)")
    @PostMapping("/editForSave")
    public Result editForSave(@RequestBody SysBigClientAddReq req) {
        int addType = 1;
        return bigClientService.editBigClient(req, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "编辑大客户(走审批流程)", notes = "编辑大客户(走审批流程)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "编辑大客户(走审批流程)", operDesc = "编辑大客户(走审批流程)")
    @PostMapping("/editForApproval")
    public Result editForApproval(@RequestBody SysBigClientAddReq req) {
        int addType = 2;
        return bigClientService.editBigClient(req, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "大客户管理列表查询", notes = "大客户管理列表查询")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "大客户管理列表查询", operDesc = "大客户管理列表查询")
    @PostMapping("/queryClient")
    public Result<PageResponse<SysBigClientQueryListDTO>> queryClient(@RequestBody SysBigClientQueryListReq req) {
        PageResponse<SysBigClientQueryListDTO> clients = bigClientService.queryList(req, getCurrentUser(getRequest()));
        return Result.success(clients);
    }

    @ApiOperation(value = "大客户管理列表查询(验证)", notes = "大客户管理列表查询(验证)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "大客户管理列表查询", operDesc = "大客户管理列表查询")
    @PostMapping("/queryClientPer")
    public Result<PageResponse<SysBigClientQueryListDTO>> queryClientPer(@RequestBody SysBigClientQueryListReq req) {
        PageResponse<SysBigClientQueryListDTO> clients = bigClientService.queryListPer(req, getCurrentUser(getRequest()));
        return Result.success(clients);
    }

    @ApiOperation(value = "大客户AND代理商联合查询", notes = "大客户AND代理商联合查询")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = true, value = "名称")
    })
    @GetMapping("/queryBigClientAndAgent")
    public Result<List<QueryBigClientAndAgentDTO>> queryBigClientAndAgent(@RequestParam(value = "name") String name) {
        return Result.success(bigClientService.queryBigClientAndAgent(name));
    }

    @ApiOperation(value = "大客户审核", notes = "大客户审核")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "大客户审核", operDesc = "大客户审核")
    @PostMapping("/approval")
    public Result approval(@RequestBody @Valid SysBigClientApprovalReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return bigClientService.approval(req);
    }

    @ApiOperation(value = "修改大客户状态", notes = "修改大客户状态")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "修改大客户状态", operDesc = "修改大客户状态")
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody @Valid SysBigClientUpdateStatusReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return bigClientService.updateStatus(req);
    }

    @ApiOperation(value = "删除大客户", notes = "删除大客户")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @GetMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        return bigClientService.deleteById(id);
    }

}
